package org.xworker.web;

import org.xmeta.ActionContext;
import xworker.lang.executor.Executor;
import xworker.lang.util.path.PathResult;
import xworker.web.servlet.*;

import java.io.IOException;
import java.util.Enumeration;

public class LogFilter implements Filter {
    private static final String TAG = LogFilter.class.getName();

    @Override
    public void doFilter(PathResult<?> pathResult, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws IOException, ServletException {
        Executor.info(TAG, "Request: " + httpServletRequest.getRequestURI());
        Executor.info(TAG, "Content-Type: " + httpServletRequest.getContentType());
        Enumeration<String> headerNames = httpServletRequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            Executor.info(TAG, "Header:" + name + "=" + httpServletRequest.getHeader(name));
        }

        filterChain.doFilter(pathResult, httpServletRequest, httpServletResponse);

        Executor.info(TAG, "Response: Content-Type: " + httpServletResponse.getContentType());
    }

    //org.xworker.ControllMappings/@LogFilter/@actions/@createFilter
    public static LogFilter create(ActionContext actionContext) {
        return new LogFilter();
    }

    @Override
    public String getName() {
        return "LogFilter";
    }
}
